package com.summer.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

import com.sun.istack.internal.Nullable;

public class AnnotationTest {

	
	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		
		TestObj o = new TestObj();

		Method method = TestObj.class.getDeclaredMethod("doTest", String.class, String.class, String.class, String.class);
		List<ParamsWrap> parameterAnnotations = getParameterAnnotationBy(method, ParamsWrap.class);
		
		for(Annotation wrap : parameterAnnotations) {
			if(wrap instanceof ParamsAlias) {

				System.out.println("=========ParamsAlias");
			}
			System.out.println("=========" + wrap.toString());
		}
	}
	
	public static <T extends Annotation> List<T> getParameterAnnotationBy(Method method, Class<T> annotationClass) {
		
		
		
		Parameter[] parameters = method.getParameters();
		
		for(Parameter parameter : parameters) {
			if(parameter.isAnnotationPresent(ParamsAlias.class)) {
				System.out.println("annotationClass");
			}
		}
		
		
		
		
		
		Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations == null || parameterAnnotations.length == 0) {
            return null;
        }
        List<T> dstAnnotations = new ArrayList<>();
        int i = 0;
        for (Annotation[] parameterAnnotation : parameterAnnotations) {
            for (Annotation annotation : parameterAnnotation) {
//                if(annotationClass.isInstance(annotation)){
                if(annotation.annotationType().isAnnotationPresent(annotationClass)){            	
                    dstAnnotations.add((T) annotation);
                    break;
                }
            }
        }
        return dstAnnotations;
    }
	
	public static class TestObj {
		
		
		public void doTest(@ParamsAlias String alias, @ParamsUrl @ParamsAlias String url, @ParamsHead String head, @Nullable String x) {
			
		}
		
	}
	
}
